package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;

import com.heima.user.service.ApUserFollowService;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.ApThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {
    @Autowired
    private ApUserService apUserService;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Override
    public ResponseResult save(UserRelationDto dto, HttpServletRequest request) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String userId = request.getHeader("userId");
        if ("0".equals(userId)) {
            return ResponseResult.errorResult(509,"请登录后再试！");
        }
        if (dto.getOperation()==0){

//            ApThreadLocalUtil.get().getId()
        ApUser apUser = apUserService.getById(userId);
            ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(Integer.parseInt(userId));
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setIsNotice(true);

        apUserFollow.setFollowName(apUser.getName());
            save(apUserFollow);
            saveFan(dto, Integer.parseInt(userId), apUser);
            //拉取粉丝表
            List<ApUserFan> apUserFans = poll(dto);
//            HashMap<String, List<ApUserFan>> map = new HashMap<>();
//            map.put(dto.getAuthorId().toString(),apUserFans);
            cacheService.set("AuthorId"+dto.getAuthorId().toString(), JSON.toJSONString(apUserFans));


            return ResponseResult.okResult(null);
        }else {

            LambdaQueryWrapper<ApUserFan> fanLambdaQueryWrapper = new LambdaQueryWrapper<>();
            fanLambdaQueryWrapper.eq(ApUserFan::getFansId,userId);
            apUserFanMapper.delete(fanLambdaQueryWrapper);
            //拉取粉丝表
            List<ApUserFan> apUserFans = poll(dto);
            HashMap<String, List<ApUserFan>> map = new HashMap<>();
//            map.put(dto.getAuthorId().toString(),apUserFans);
            cacheService.set("AuthorId"+dto.getAuthorId().toString(), JSON.toJSONString(apUserFans));
            LambdaQueryWrapper<ApUserFollow> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApUserFollow::getUserId,userId);
            apUserFollowMapper.delete(wrapper);
            return ResponseResult.okResult(null);
        }


    }
    //粉丝保存
    private void saveFan(UserRelationDto dto, Integer userId, ApUser apUser) {
        ApUserFan apUserFan = new ApUserFan();
        apUserFan.setUserId(dto.getAuthorId());
        apUserFan.setFansId(userId);
        apUserFan.setFansName(apUser.getName());
        apUserFan.setCreatedTime(new Date());
        apUserFan.setIsDisplay(true);
        apUserFan.setIsShieldComment(true);
        apUserFan.setIsShieldLetter(true);
        apUserFanMapper.insert(apUserFan);
    }
    //拉取粉丝表
    private List<ApUserFan>  poll(UserRelationDto dto){
        LambdaQueryWrapper<ApUserFan> apUserFanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apUserFanLambdaQueryWrapper.eq(ApUserFan::getUserId,dto.getAuthorId());


        List<ApUserFan> apUserFans = apUserFanMapper.selectList(apUserFanLambdaQueryWrapper);
        return apUserFans;
    }
}
